<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }" style="margin-left: 20px">社区维修管理</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">故障报修</a></el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入报修名称..." suffix-icon="el-icon-search" class="ml-5"
                v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="seachKehu">搜索</el-button>
      <el-button @click="resetForm">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="openKehudialog">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
      <el-button type="danger" @click="delbath">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
      <el-upload
          action="http://localhost:8888/api/fix/import" style="display: inline-block" :show-file-list="false"
          :accept="xls" :on-success="impOK" :on-error="imperr"
      >
        <el-button type="primary" class="ml-5">导入<i class="el-icon-bottom" style="margin-left: 2px"></i></el-button>
      </el-upload>
      <el-button type="primary" @click="exp" class="ml-5">导出<i class="el-icon-top" style="margin-left: 2px"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="50" fixed></el-table-column>
      <el-table-column prop="orderId" label="报修单号" width="130px">
      </el-table-column>
      <el-table-column prop="name" label="报修名称" width="100px">
      </el-table-column>
      <el-table-column prop="door" label="门牌号" width="100px">
      </el-table-column>
      <el-table-column prop="content" label="报修内容" width="120px">
      </el-table-column>
      <el-table-column prop="note" label="报修备注" width="130px">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="80px">
      </el-table-column>
      <el-table-column prop="phone" label="手机" width="120px">
      </el-table-column>
      <el-table-column prop="fixDate" label="报修日期" width="160px">
      </el-table-column>
      <el-table-column prop="solve" label="解决情况" width="150px">
        <template v-slot="{ row }">
          <span v-if="row.solve == '1'"><el-button type="danger" plain  @click="open(row)">未解决</el-button></span>
          <span v-if="row.solve == '2'"><el-button type="success" plain @click="dialog(row)">已解决</el-button></span>
        </template>
      </el-table-column>
      <el-table-column label="操作"  fixed="right" width="200px">
        <template slot-scope="scope">
          <el-button type="primary"  @click="updatekehu(scope.row)"><i class="el-icon-edit"></i>编辑</el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定要删除这条记录吗？"
              @confirm="deletekehu(scope.row.id)"
          >
            <el-button type="danger" slot="reference"><i class="el-icon-remove-outline"></i>删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10,15,20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>


    <el-dialog title="在线报修" :visible.sync="dialogFormVisible" width="50%">
      <el-form :inline="true"  :model="form" label-position="left">
        <el-form-item label="报修名称" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.name" autocomplete="off" placeholder="请输入报修名称" style="width: 220px"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="门牌号" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.door" autocomplete="off" placeholder="请输入门牌号" style="width: 220px"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="报修内容" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.content" autocomplete="off" placeholder="请输入保修内容" style="width: 220px"  type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="保修备注" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.note" autocomplete="off" placeholder="请输入备注" style="width: 220px"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="姓名" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.username" autocomplete="off" placeholder="请输入姓名" style="width: 220px"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="手机号" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.phone" autocomplete="off" placeholder="请输入手机号" style="width: 220px" maxlength="11"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="保修日期" label-width="100px">
          <el-col :span="16">
            <el-date-picker
                v-model="form.fixDate"
                type="date"
                placeholder="选择日期"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-col>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveKehu">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="反馈信息" :visible.sync="dialogFormVisible2" width="30%">
      <el-form :model="form">
        <el-form-item label="内容：">
          <el-input v-model="form.relf" autocomplete="off" type="textarea"
                    :autosize="{ minRows: 2, maxRows: 4}"   :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible2 = false">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "fix",
  data(){
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: "",
      form:{
        id:"",
        orderId:"",
        name:"",
        username:"",
        door:"",
        content:"",
        fixDate:"",
        note:"",
        phone:"",
        solve:"",
        relf:""
      },
      dialogFormVisible: false,
      dialogFormVisible2: false,
      headerBg: 'headerBg',
      multipleSelection: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    dialog(row){
      this.dialogFormVisible2=true;
      this.form=row;
    },
    solveOk(){
      request.post("fix/update",this.form).then(res =>{
        if (res.code=="200"){
          this.load()
          this.$message.success("反馈成功")
        }else {
          this.$message.error("反馈失败")
        }
      })
    },
    open(data) {
      this.$prompt('请输入回复内容', '回复', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'textarea'
      }).then(({ value }) => {
        if (value==null) { // 检查输入内容是否为空
          this.$message({
            type: 'warning',
            message: '请先输入回复内容'
          });
        } else {
          this.form = data;
          this.form.solve = '2';
          this.form.relf = value;
          this.solveOk();
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });
      });
    },
    imperr(){
      this.$message.error("文件导入失败")
    },
    impOK(){
      this.$message.success("文件导入成功")
      this.load()
    },
    exp(){
      window.open("http://localhost:8888/api/fix/export");
    },
    delbath(){
      let ids=this.multipleSelection.map(v =>v.id)
      request.post("fix/deletebatch",ids).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    deletekehu(id) {
      console.log(id)
      request.post("fix/delete",id).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    updatekehu(row){
      this.form=row
      this.dialogFormVisible=true
    },
    saveKehu(){
      this.dialogFormVisible = false
      if (this.form.id){
        request.post("fix/update",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("修改信息成功")
          }else {
            this.$message.error("修改信息失败")
          }
        })
      }else {
        this.form.solve=1
        request.post("fix",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("新增报修成功")
          }
        })
      }

    },
    openKehudialog(){
      this.form={}
      this.dialogFormVisible=true
    },
    resetForm() {
      this.name = ""
      this.load()
    },
    seachKehu() {
      this.load()
    },
    handleCurrentChange(data) {
      this.pageNum = data
      this.load()
    },
    handleSizeChange(data) {
      this.pageSize = data
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    load() {
      request.get("fix/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name
        }
      }).then(res => {
        if (res.code=="200"){
          this.tableData = res.data.records
          this.total = res.data.total
        }

      })
    }
  }

}
</script>

<style scoped>

</style>